package cn.edu.gzhu.workOrderManagement.controller;

import cn.edu.gzhu.workOrderManagement.constants.MessageConstant;
import cn.edu.gzhu.workOrderManagement.pojo.dto.workOrder.*;
import cn.edu.gzhu.workOrderManagement.pojo.dto.workOrderHistory.WorkOrderHistoryListDto;
import cn.edu.gzhu.workOrderManagement.pojo.vo.WorkOrderChartAnyVo;
import cn.edu.gzhu.workOrderManagement.pojo.vo.WorkOrderDetailVo;
import cn.edu.gzhu.workOrderManagement.pojo.vo.WorkOrderListVo;
import cn.edu.gzhu.workOrderManagement.pojo.vo.WorkOrderStatusAnyVo;
import cn.edu.gzhu.workOrderManagement.properties.JwtProperties;
import cn.edu.gzhu.workOrderManagement.result.Result;
import cn.edu.gzhu.workOrderManagement.service.WorkOrderService;
import cn.edu.gzhu.workOrderManagement.util.JwtUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.jsonwebtoken.Claims;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

import static cn.edu.gzhu.workOrderManagement.util.MyBeanUtil.setEmptyStringsToNull;


@RestController
@RequestMapping("/workOrder")
@Tag(name = "工单管理接口")
public class WorkOrderController {
    @Autowired
    WorkOrderService workOrderService;
    @Autowired
    JwtProperties jwtProperties;
    /**
     * 创建工单接口，前端解析excel数据然后传递给后端，或者用户直接填写表单
     * @param token
     * @param workOrderBatchCreateDto
     * @return
     */
    //前端负责返回详细的校验提示，把校验规则告诉前端
    @Operation(summary = "创建工单")
    @PostMapping("/create")
    public Result createWorkOrder(@RequestHeader String token,@Validated @RequestBody  WorkOrderBatchCreateDto workOrderBatchCreateDto) {
        List<WorkOrderCreateDto> workOrderCreateDtoList = workOrderBatchCreateDto.getWorkOrderCreateDtoList();

        Boolean isSuccess = workOrderService.createWorkOrder(workOrderCreateDtoList);
        if (!isSuccess) {
            return Result.error(MessageConstant.PARAMETER_ERROR);
        }
        return Result.success();
    }

    /**
     * 按照条件分页查询工单
     *
     * @param token
     * @param workOrderListDto
     * @return
     */
    @Operation(summary = "工单分页查询")
    @GetMapping("/list")
    public Result<IPage<WorkOrderListVo>> listWorkOrderByConditions(@RequestHeader String token, @Validated WorkOrderListDto workOrderListDto) {
        //分页参数不能为空，起始页数从1开始，页面长度不超过100
        setEmptyStringsToNull(workOrderListDto);
        IPage<WorkOrderListVo> workOrderVoIPage = workOrderService.listWorkOrder(workOrderListDto);

        return Result.success(workOrderVoIPage);
    }

    @Operation(summary = "工单历史记录分页查询")
    @GetMapping("/listHistory")
    public Result<IPage<WorkOrderListVo>> listWorkOrderHistory(@RequestHeader String token, @Validated WorkOrderHistoryListDto workOrderHistoryListDto) {
        Claims claims = JwtUtil.parseJWT(jwtProperties.getUserSecretKey(), token);
        String username = (String) claims.get("username");
        IPage<WorkOrderListVo> workOrderVoIPage = workOrderService.listHistoryWorkOrder(username,workOrderHistoryListDto);

        return Result.success(workOrderVoIPage);
    }

    @Operation(summary = "工单模糊搜索")
    @GetMapping("/fuzzySearch")
    public Result<IPage<WorkOrderListVo>> fuzzySearch(@RequestHeader String token, @Validated FuzzySearchDto fuzzySearchDto){

        IPage<WorkOrderListVo> workOrderVoIPage = workOrderService.fuzzySearch(fuzzySearchDto);

        return Result.success(workOrderVoIPage);
    }

    /**
     * 更新工单，可更新的有工单的名称，目标组织，预期结束时间，状态，用户自定义字段
     * @param token
     * @param workOrderUpdateDto
     * @return
     */
    @Operation(summary = "工单更新")
    @PostMapping("/update")
    public Result updateWorkOrder(@RequestHeader String token, @RequestBody @Validated WorkOrderUpdateDto workOrderUpdateDto) {
        Claims claims = JwtUtil.parseJWT(jwtProperties.getUserSecretKey(), token);
        String username = (String) claims.get("username");
        setEmptyStringsToNull(workOrderUpdateDto);
        Boolean res = workOrderService.updateWorkOrder(username,workOrderUpdateDto);
        if (res) {
            return Result.success();
        }
        return Result.error(MessageConstant.PARAMETER_ERROR);
    }

    @Operation(summary = "工单重诉")
    @PostMapping("/repeat")
    public Result repeatWorkOrder(@RequestHeader String token,@RequestBody @Validated WorkOrderRepeatDto workOrderRepeatDto) {

        Boolean res = workOrderService.repeatWorkOrder(workOrderRepeatDto);
        if (!res){
            return Result.error(MessageConstant.PARAMETER_ERROR);
        }
        return Result.success();
    }

    /**
     * 从数据库删除工单，只有管理员可以操作，一般的用户只能把自的工单状态更新为关闭
     * @param token
     * @param uuids
     * @return
     */
    @Operation(summary = "总部商务删除工单")
    @PostMapping("/delete")
    public Result deleteWorkOrder(@RequestHeader String token,@RequestBody List<String> uuids) {
        if (uuids==null||uuids.isEmpty()) {
            return Result.error(MessageConstant.PARAMETER_ERROR);
        }
        Boolean res = workOrderService.deleteWorkOrder(uuids);
        if (!res){
            return Result.error(MessageConstant.PARAMETER_ERROR);
        }
        return Result.success();
    }

    @Operation(summary = "总部商务修改工单")
    @PostMapping("/busUpdate")
    public Result busUpdateWorkOrder(@RequestHeader String token, @RequestBody WorkOrderBusUpdateDto workOrderBusUpdateDto) {
        Claims claims = JwtUtil.parseJWT(jwtProperties.getUserSecretKey(), token);
        String username = (String) claims.get("username");
        setEmptyStringsToNull(workOrderBusUpdateDto);
        Boolean res =   workOrderService.busUpdate(username,workOrderBusUpdateDto);
        if (!res){
        return Result.error(MessageConstant.PARAMETER_ERROR);
        }
        return Result.success();

    }

    @Operation(summary = "获取工单详情")
    @GetMapping("/getDetail")
    public Result getDetail(@RequestHeader String token,@NotBlank String uuid) {

        WorkOrderDetailVo workOrderDetail = workOrderService.getWorkOrderDetail(uuid);
        if (workOrderDetail==null){
            return Result.error(MessageConstant.PARAMETER_ERROR);
        }
        return Result.success(workOrderDetail);
    }

    // 按日/月统计不同状态工单数量
    @Operation(summary = "工单状态统计")
    @GetMapping("/statusCount")
    public Result<List<WorkOrderStatusAnyVo>> statusCount(@RequestHeader String token, @NotBlank String date) {
        List<WorkOrderStatusAnyVo> workOrderStatusAnyVoList = workOrderService.getWorkOrderStatusAnalysis(date);
        return Result.success(workOrderStatusAnyVoList);
    }

    // 按日/月统计不同类型工单数量
    @Operation(summary = "工单状态统计")
    @GetMapping("/typeCount")
    public Result<List<WorkOrderStatusAnyVo>> typeCount(@RequestHeader String token, @NotBlank String date) {
        List<WorkOrderStatusAnyVo> workOrderStatusAnyVoList = workOrderService.getWorkOrderTypeAnalysis(date);
        return Result.success(workOrderStatusAnyVoList);
    }

    // 统计近n月工单数量
    @Operation(summary = "工单数量统计")
    @GetMapping("/statisticsCount")
    public Result<List<WorkOrderChartAnyVo>> statusCount(@RequestHeader String token, @NotBlank Integer month) {
        List<WorkOrderChartAnyVo> workOrderChartAnyVoList = workOrderService.getWorkOrderChartAnalysis(month);
        return Result.success(workOrderChartAnyVoList);
    }

}
